package com.shouke.jmeter.plugin.elementconfig;

import org.apache.jmeter.testbeans.BeanInfoSupport;
import org.apache.jmeter.testbeans.gui.TypeEditor;

import java.beans.PropertyDescriptor;

public class RabbitMQConnectConfigElementBeanInfo extends BeanInfoSupport {
    private static final String[] BOOLEAN_OPTIONS = new String[2];

    /**
     * 设置列表选项
     */
    static {
        BOOLEAN_OPTIONS[0] = "true";
        BOOLEAN_OPTIONS[1] = "false";
    }

    public RabbitMQConnectConfigElementBeanInfo(){
        super(RabbitMQConnectConfigElement.class);

        // 创建属性组，实现配置元件界面“组框”的效果
        createPropertyGroup("Variable Name bound to rabbitMQ connection", new String[] {"rabbitMQConnVariableName"});
        // 添加GUI字段
        PropertyDescriptor ClientVariableNamePropDesc =  property("rabbitMQConnVariableName");
        ClientVariableNamePropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        ClientVariableNamePropDesc.setValue(DEFAULT, "rabbitMQConn");
        ClientVariableNamePropDesc.setDisplayName("Variable Name");
        ClientVariableNamePropDesc.setShortDescription("Variable Name to use in RabbitMQ Pub Sampler or Sub Sampler");

        createPropertyGroup("Connection Configs", new String[] {
                "host",
                "port",
                "username",
                "password",
                "isAutoRecoveryEnabled",
                "networkRecoveryInterval",
                "virtualHost",
                "connectionTimeout",
                "handshakeTimeout",
                "shutdownTimeout"
        });

        // 在Jmeter GUI 组框中添加字段及设置
        PropertyDescriptor connectionConfigPropDesc =  property("host");
        connectionConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        connectionConfigPropDesc.setValue(DEFAULT, "127.0.0.1");
        connectionConfigPropDesc.setDisplayName("Host");
        connectionConfigPropDesc.setShortDescription("host of rabbitmq server");

        connectionConfigPropDesc =  property("port");
        connectionConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        connectionConfigPropDesc.setValue(DEFAULT, 5672);
        connectionConfigPropDesc.setDisplayName("RabbitMQ Port");
        connectionConfigPropDesc.setShortDescription("port of rabbitMQ");

        connectionConfigPropDesc =  property("username");
        connectionConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        connectionConfigPropDesc.setValue(DEFAULT, "");
        connectionConfigPropDesc.setDisplayName("RabbitMQ Username");
        connectionConfigPropDesc.setShortDescription("username for RabbitMQ login");

        connectionConfigPropDesc =  property("password");
        connectionConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        connectionConfigPropDesc.setValue(DEFAULT, "");
        connectionConfigPropDesc.setDisplayName("User Password");
        connectionConfigPropDesc.setShortDescription("password for RabbitMQ user login");

        connectionConfigPropDesc =  property("isAutoRecoveryEnabled", TypeEditor.ComboStringEditor);
        connectionConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        connectionConfigPropDesc.setValue(DEFAULT, BOOLEAN_OPTIONS[0]);
        connectionConfigPropDesc.setValue(NOT_OTHER, Boolean.TRUE);
        connectionConfigPropDesc.setValue(TAGS, BOOLEAN_OPTIONS);
        connectionConfigPropDesc.setDisplayName("Auto Reconnect When Connection Lost");
        connectionConfigPropDesc.setShortDescription("if auto connect server when connection lost enabled");

        connectionConfigPropDesc =  property("networkRecoveryInterval");
        connectionConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        connectionConfigPropDesc.setValue(DEFAULT, 5000);
        connectionConfigPropDesc.setDisplayName("Network Recovery Interval(milliseconds)");
        connectionConfigPropDesc.setShortDescription("interval for connecting to rabbitMQ server when connection lost");

        connectionConfigPropDesc =  property("virtualHost");
        connectionConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        connectionConfigPropDesc.setValue(DEFAULT, "/");
        connectionConfigPropDesc.setDisplayName("VirtualHost");
        connectionConfigPropDesc.setShortDescription("rabbitMQ vitrual host");

        connectionConfigPropDesc =  property("connectionTimeout");
        connectionConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        connectionConfigPropDesc.setValue(DEFAULT, 60000);
        connectionConfigPropDesc.setDisplayName("Connection Timeout(milliseconds)");
        connectionConfigPropDesc.setShortDescription("timeout for connecting to rabbitMQ server");

        connectionConfigPropDesc =  property("handshakeTimeout");
        connectionConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        connectionConfigPropDesc.setValue(DEFAULT, 10000);
        connectionConfigPropDesc.setDisplayName("Handle Shake Timeout(milliseconds)");
        connectionConfigPropDesc.setShortDescription("timeout for ssl handshake when connecting to rabbitMQ server");

        connectionConfigPropDesc =  property("shutdownTimeout");
        connectionConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        connectionConfigPropDesc.setValue(DEFAULT, 0);
        connectionConfigPropDesc.setDisplayName("Shutdown Time Out(milliseconds)");
        connectionConfigPropDesc.setShortDescription("max wait time before client shutdown");
    }
}

